<!DOCTYPE html>
<html>
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
const ORIGIN_CLEAN_URL = 'http://127.0.0.1:8000/security/resources/abe.png'
const NON_ORIGIN_CLEAN_URL = 'http://localhost:8000/security/resources/abe.png'

async function createImageBitmapFor(url) {
  const img = document.createElement('img');
  img.src = url;
  try {
    await new Promise((resolve, reject) => {
      img.addEventListener('load', resolve, {once: true});
      img.addEventListener('error', reject, {once: true});
    });
  } finally {
    img.remove();
  }
  return createImageBitmap(img);
}

promise_test(async (t) => {
  const FEATURE = 3056;

  const bitmap = await createImageBitmapFor(ORIGIN_CLEAN_URL);

  assert_false(internals.isUseCounted(document, FEATURE));

  postMessage(bitmap, '*');

  assert_true(internals.isUseCounted(document, FEATURE));
}, 'origin-clean, serialization');

promise_test(async (t) => {
  const FEATURE = 3057;

  const bitmap = await createImageBitmapFor(NON_ORIGIN_CLEAN_URL);

  assert_false(internals.isUseCounted(document, FEATURE));

  assert_throws_dom('DataCloneError', () => postMessage(bitmap, '*'));
}, 'non-origin-clean, serialization');

promise_test(async (t) => {
  const FEATURE = 3058;

  const bitmap = await createImageBitmapFor(ORIGIN_CLEAN_URL);

  assert_false(internals.isUseCounted(document, FEATURE));

  postMessage(bitmap, '*', [bitmap]);

  assert_true(internals.isUseCounted(document, FEATURE));
}, 'origin-clean, transfer');

promise_test(async (t) => {
  const FEATURE = 3059;

  const bitmap = await createImageBitmapFor(NON_ORIGIN_CLEAN_URL);

  assert_false(internals.isUseCounted(document, FEATURE));

  assert_throws_dom('DataCloneError', () => postMessage(bitmap, '*', [bitmap]));
}, 'non-origin-clean, transfer');
</script>
</body>
</html>
